﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IMasContactService
    {
        #region Contact
        [OperationContract]
        IEnumerable<MasContact> SelectContact(MasContact criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListContact(MasContact criteriaModel = null);

        [OperationContract]
        bool CheckDuplicateTaxCode(string TaxCode);

        [OperationContract]
        int InsertContact(MasContact model, int createByUserId);

        [OperationContract]
        void UpdateContact(MasContact model, int updateByUserId);

        [OperationContract]
        void DeleteContact(int id, int deleteByUserId);
        #endregion

        #region ContactBank
        [OperationContract]
        IEnumerable<MasContactBank> SelectContactBank(MasContactBank criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListContactBank(MasContactBank criteriaModel = null);

        [OperationContract]
        int InsertContactBank(MasContactBank model, int createByUserId);

        [OperationContract]
        void UpdateContactBank(MasContactBank model, int updateByUserId);

        [OperationContract]
        void DeleteContactBank(int id, int deleteByUserId);
        #endregion

        #region ContactPartner
        [OperationContract]
        IEnumerable<MasContactPartner> SelectContactPartner(MasContactPartner criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListContactPartner(MasContactPartner criteriaModel = null);

        [OperationContract]
        int InsertContactPartner(MasContactPartner model, int createByUserId);

        [OperationContract]
        void UpdateContactPartner(MasContactPartner model, int updateByUserId);

        [OperationContract]
        void DeleteContactPartner(int id, int deleteByUserId);
        #endregion
    }

    public class MasContactService : IMasContactService
    {
        #region Contact

        public IEnumerable<MasContact> SelectContact(MasContact criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContact> query = context.MasContacts;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<SelectListItem> SelectListContact(MasContact criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContact> query = context.MasContacts;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.NameTh, Value = model.ConId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public bool CheckDuplicateTaxCode(string TaxCode)
        {
            bool bFound = false;

            try
            {
                using (var context = new GERPContext(false))
                {
                    var count = context.MasContacts.Where(m => m.TaxCode.Replace("-", "").Replace(" ", "").ToLower() == TaxCode.Replace("-", "").Replace(" ", "").ToLower()).Count();

                    bFound = count > 0;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return bFound;
        }

        public int InsertContact(MasContact model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasContacts.Add(model);
                    context.SaveChanges();

                    return model.ConId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateContact(MasContact model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteContact(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasContacts.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region ContactBank

        public IEnumerable<MasContactBank> SelectContactBank(MasContactBank criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContactBank> query = context.MasContactBanks;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<SelectListItem> SelectListContactBank(MasContactBank criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContactBank> query = context.MasContactBanks.Include("MasBankBranch.Bank");

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.AccountCode + " - " + model.MasBankBranch.Bank.Name + " / " + model.MasBankBranch.BranchName, Value = model.ConBankId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertContactBank(MasContactBank model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasContactBanks.Add(model);
                    context.SaveChanges();

                    return model.ConBankId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateContactBank(MasContactBank model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteContactBank(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasContactBanks.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region ContactPartner

        public IEnumerable<MasContactPartner> SelectContactPartner(MasContactPartner criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContactPartner> query = context.MasContactPartners;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<SelectListItem> SelectListContactPartner(MasContactPartner criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasContactPartner> query = context.MasContactPartners;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.CitizenId + " - " + model.Name, Value = model.ConPartId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertContactPartner(MasContactPartner model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasContactPartners.Add(model);
                    context.SaveChanges();

                    return model.ConPartId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateContactPartner(MasContactPartner model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteContactPartner(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasContactPartners.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
